﻿namespace SP2010.Enhancements.SEO
{
    using System;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Text.RegularExpressions;

    internal class WhitespaceFilter : Stream
    {
        private readonly Stream _stream;
        private static readonly Regex Pattern = new Regex(@"^\s+", RegexOptions.Compiled | RegexOptions.Multiline);

        public WhitespaceFilter(Stream stream)
        {
            try
            {
                this._stream = stream;
            }
            catch
            {
                throw;
            }
        }

        public override void Close()
        {
            try
            {
                this._stream.Close();
            }
            catch
            {
                throw;
            }
        }

        public override void Flush()
        {
            try
            {
                this._stream.Flush();
            }
            catch
            {
                throw;
            }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            int num;
            try
            {
                num = this._stream.Read(buffer, offset, count);
            }
            catch
            {
                throw;
            }
            return num;
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            long num;
            try
            {
                num = this._stream.Seek(offset, origin);
            }
            catch
            {
                throw;
            }
            return num;
        }

        public override void SetLength(long value)
        {
            try
            {
                this._stream.SetLength(value);
            }
            catch
            {
                throw;
            }
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            byte[] dst = new byte[count];
            Buffer.BlockCopy(buffer, offset, dst, 0, count);
            string s = Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace(Encoding.Default.GetString(buffer), "[a-zA-Z]+#", "#"), @"[\n\r]+\s*", string.Empty), @"\s+", " "), @"\s?([:,;{}])\s?", "$1").Replace(";}", "}"), @"([\s:]0)(px|pt|%|em)", "$1"), @"/\*[\d\D]*?\*/", string.Empty);
            byte[] bytes = Encoding.Default.GetBytes(s);
            this._stream.Write(bytes, 0, bytes.GetLength(0));
        }

        public override bool CanRead
        {
            get
            {
                return true;
            }
        }

        public override bool CanSeek
        {
            get
            {
                return true;
            }
        }

        public override bool CanWrite
        {
            get
            {
                return true;
            }
        }

        public override long Length
        {
            get
            {
                return 0L;
            }
        }

        public override long Position { get; set; }
    }
}

